<template>
	<view class="mainbgc">
		<!--日期 -->
		<!-- 开始时间 -->
		<u-datetime-picker :show="shows" v-model="value1" title="开始时间" mode="date" @confirm="confirmstart"
			@cancel="shows=false" ></u-datetime-picker>
		<!-- 结束时间 -->
		<u-datetime-picker :show="showss" v-model="value2"  :minDate="mintime" title="结束时间" mode="date" @confirm="confirmend"
			@cancel="showss=false"></u-datetime-picker>
		<header-top :messageInfo="{typeFlag:true,message:'业务销售排行',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content">
					<view class="nostrongtext" @click="pop_show=true">筛选</view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding">
			<view class="titletext ffffff tabletitle mainpadding2">{{datetext}}业务销售排行</view>
			<view class="mainpadding ffffff">
				<!-- 筛选 -->
				<view class="flexbetween">
					<view class="flexleft">
						<view :class="datecurrent==1?'select':'noselect'" style="margin-right: 15rpx;"
							@click="switchDate(1)">年</view>
						<view :class="datecurrent==2?'select':'noselect'" style="margin-right: 15rpx;"
							@click="switchDate(2)">月</view>
						<view :class="datecurrent==3?'select':'noselect'" style="margin-right: 15rpx;"
							@click="switchDate(3)">日</view>
					</view>
					<view class="flexleft selecttime_box radius" @click="shows = true">
						<view class="selecttime" style="margin-right: 20rpx;"></view>
						<view class="selecttime_text">{{startime==""?"自定义时间":startime+"-"+endtime}}</view>
					</view>
				</view>
				<!-- 柱状图图表 -->
				<view class="charts-box2">
					<qiun-data-charts type="column" :opts="opts" :chartData="chartData" />
				</view>
			</view>
		</view>
		<view class="needzdybot">
			<view class="strongtext">
				<text class="titletext textliner">详细数据</text>
			</view>
			<view class="mainpadding_top radius ffffff">
				<view class="flexbetween">
					<view class="titletext flex2">名称</view>
					<view class="titletext flex1">订单数</view>
					<view class="titletext flex1">部门</view>
					<view class="titletext flex2 flexs">销售净额</view>
				</view>
				<view class="flexcenter" v-if="!list.total">
					<view class="placeholderimage"></view>
				</view>
				<view class="flexbetween margin_top" :class="index==list.data.length-1?'':'itembox'" v-for="(item,index) in list.data" :key="item.id">
					<view class="flexleft flex2">
						<view class="images" v-if="index==0">
							<image src="../../../static/image/system/diyi.png" mode=""></image>
						</view>
						<view class="images" v-if="index==1">
							<image src="../../../static/image/system/dier.png" mode=""></image>
						</view>
						<view class="images" v-if="index==2">
							<image src="../../../static/image/system/disan.png" mode=""></image>
						</view>
						<view class="nostrongtext">{{item.username}}</view>
					</view>
					<view class="nostrongtext flex1">{{item.sale_num}}</view>
					<view class="nostrongtext flex1">{{item.staffbm.name}}</view>
					<view class="rightmsg flexright flex2 flexs">
						<view class="nostrongtext" :style="{color:Number(item.xc_price)>=0?'':'#ff0000'}">{{item.xc_price}}</view>
						<view class="smalltext">销:{{item.saleorder_sum || 0}} 退:{{item.saleth_price || 0}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tab_bottom ffffff flexbetween">
			<view class="strongtext flex2">合计</view>
			<view class="strongtext flex1">{{alldata.total_sale_num}}</view>
			<view class="rightmsg flexright flex2 flexs">
				<view class="strongtext" :style="{color:Number(alldata.total_xc_price)>=0?'#0BBDCB':'#ff0000'}">{{alldata.total_xc_price}}</view>
				<view class="smalltext" style="color: #0BBDCB;">销:{{alldata.total_sale_price}} 退:{{alldata.total_saleth_price}}</view>
			</view>
		</view>
		<u-popup round="20" :show="pop_show" @close="close">
			<view class="mainpadding_top ffffff">
				<view class="flexcenter">
					<view class="titletext">筛选</view>
				</view>
				<view class="itemscreen flexleft margin_top" @click="khPianQuShow=true">
					<view class="strongtext minwidth">片区</view>
					<view class="rightscreen flexbetween">
						<input type="text" disabled="true" placeholder="请选择客户片区" v-model="khPianQuText">
						<view class="righticon2"></view>
					</view>
				</view>
				<view class="itemscreen flexleft margin_top" @click="ckBuMenShow=true">
					<view class="strongtext minwidth">部门</view>
					<view class="rightscreen flexbetween">
						<input type="text" disabled="true" placeholder="请选择部门" v-model="ckBuMenText">
						<view class="righticon2"></view>
					</view>
				</view>
				<view class="flexbetween">
					<view class="surebtn reset flexcenter" style="margin-top: 60rpx;" @click="resetbtn">重置</view>
					<view class="surebtn flexcenter" style="margin-top: 60rpx;" @click="sureBtn">确认</view>
				</view>
			</view>
		</u-popup>
		<selectCKBuMen :message="{ckBuMenShow:ckBuMenShow}" @ckBuMenSure="ckBuMenSure"></selectCKBuMen><!-- 仓库部门 -->
		<selectKHPianQu :message="{khPianQuShow:khPianQuShow}" @khPianQuSure="khPianQuSure"></selectKHPianQu>
		<!-- 客户片区 -->
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				shows: false,
				showss: false,
				value1: Number(new Date()),
				value2: Number(new Date()),
				mintime: Number(new Date()), //最小时间
				// 自定义时间
				startime: this.benyue(),
				endtime: this.riqi(0),
				datecurrent: 2,
				chartData: {},
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				datetext: '本月',
				alldata: {},
				opts: {
					dataLabel: true, //是否显示图表区域顶部的总数据
					color: ["#0bbdcb", "#99C1FF", "#2C5FAE", "#99C1FF", "#2C5FAE", "#99C1FF", "#2C5FAE", "#99C1FF",
						"#2C5FAE"
					],
					padding: [30, 10, 5, 10],
					legend: { //图例配置
						show: false, //是否显示图例配置
						fontColor: "#fff", //字体颜色
						position: "top",
						float: "right",
					},
					xAxis: { //x轴
						axisLine: false, //x轴坐标轴线是否绘制
						disableGrid: true, //true不显示横线线，false显示横线
						fontColor: "#333", //文字颜色
						rotateLabel: true, //文字旋转
						rotateAngle: 40, //旋转角度
						format: 'Xdemo'

					},
					yAxis: { //y轴
						disabled: true, //是否绘制y轴
						disableGrid: false, //true不显示横线线，false显示横线
						gridType: "dash", //横线类型
						dashLength: 16, //当为虚线时虚线的长度
						data: [{
							min: 0, //起始值
							fontColor: "#999",
							axisLine: false, //y轴坐标轴线是否绘制
						}],
					},
					extra: {
						column: {
							type: "group",
							width: 30, //每个柱状得宽度
							activeBgColor: "#fff",
							activeBgOpacity: 0.08,
							barBorderCircle: false, //顶部是否为圆角
							barBorderRadius: [5, 5, 5, 5], //自定义4个圆角半径[左上,右上,右下,左下]（请关闭上面按钮（需要将barBorderCircle设为false））
						}
					}
				},
				pop_show: false,
				// 客户片区
				khPianQuShow: false,
				khPianQuText: "",
				custom_area_id: "", //客户片区ID
				ckBuMenShow: false,
				ckBuMenText: "",
				staff_bm_id: "", //仓库部门id
			}
		},

		onLoad() {
			this.init()
		},
		methods: {
			ckBuMenSure(e) { //仓库部门
				if (e) {
					this.ckBuMenText = e.value[0].name
					this.staff_bm_id = e.value[0].id
				}
				this.ckBuMenShow = false
			},
			khPianQuSure(e) { //客户片区
				if (e) {
					this.khPianQuText = e.value[0].name
					this.custom_area_id = e.value[0].id
				}
				this.khPianQuShow = false
			},
			close() {
				this.pop_show = false
			},
			//日期选择
			confirmstart(e) {
				this.shows = false
				this.showss = true
				this.mintime = e.value
				this.startime = httpRequest.returnDate(e.value)
				// console.log(this.starttime, "开始时间确定")
			},
			confirmend(e) {
				this.showss = false
				this.endtime = httpRequest.returnDate(e.value)
				// console.log(this.endtime,"结束时间")
				this.init()
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/dhtapi/custom/staffRang', 'POST', {
					page: page || 1,
					limit: _this.limit,
					staff_bm_id: this.staff_bm_id, //片区id
					staff_area_id: this.staff_area_id, //片区id
					start_time: this.startime, //开始日期
					end_time: this.endtime //结束日期
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							_this.list = resList
							_this.list.data = resList.data;
							_this.alldata = res.data
							let msg = {
								categories: [],
								series: [{
									name: "",
									data: []
								}]
							}
							let arr = _this.list.data.slice(0,5)
							arr.forEach(item => {
								msg.categories.push(item.username)
								msg.series[0].data.push({
									value: item.saleorder_sum || 0
								})
							})
							_this.chartData = JSON.parse(JSON.stringify(msg));
						}
					}
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			// 重置
			resetbtn() {
				this.ckBuMenText = ""
				this.staff_bm_id = ""
				this.custom_area_id = ""
				this.khPianQuText = ""
				this.init()
				this.pop_show = false
			},
			sureBtn() {
				this.init()
				this.pop_show = false
			},
			//日期选择
			switchDate(value) {
				this.datecurrent = value
				if (value == 1) {
					// 获取今年第一天和今天
					this.startime = this.jinnian()
					this.endtime = this.riqi(0);
					this.datetext = "今年"
				} else if (value == 2) {
					// 获取本月第一天和今天
					this.startime = this.benyue();
					this.endtime = this.riqi(0);
					this.datetext = "本月"
				} else {
					// 获取今日
					this.startime = this.riqi(0);
					this.endtime = this.riqi(0);
					this.datetext = "今日"
				}
				this.init()
			},
			// 昨天“-1”今天“0”明天“1”
			riqi(day) {
				let dd = new Date();
				dd.setDate(dd.getDate() + day);
				let y = dd.getFullYear();
				let m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
				let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
				return y + "-" + m + "-" + d;
			},
			// 本月第一天
			benyue() {
				let y = new Date().getFullYear(); //获取年份
				let m = new Date().getMonth() + 1; //获取月份
				let d = '01'
				m = m < 10 ? '0' + m : m; //月份补 0
				return [y, m, d].join("-")
			},
			// 今年第一天
			jinnian() {
				let date1 = new Date();
				let year1 = date1.getFullYear();
				let firstMonth = year1 + '-' + '01' + '-' + '01';
				return firstMonth
			}
		}
	}
</script>

<style lang="scss" scoped>
	.surebtn {
		width: 45%;
	}

	.reset {
		background: #ebb563;
	}

	.minwidth {
		min-width: 130rpx;
	}

	.rightscreen {
		width: 100%;
	}

	.itemscreen {
		padding-bottom: 20rpx;
	}

	.images {
		width: 24rpx;
		height: 32rpx;
		margin-right: 8rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.flexs {
		text-align: right;
	}

	.flex1 {
		flex: 1;
		text-align: center;
	}

	.flex2 {
		flex: 2;
	}

	.tabletitle {
		padding-top: 30rpx;
		background-image: url("@/static/image/system/headerbgc2.png");
		background-size: 100% 100%;
	}

	// 内容开始
	.select {
		padding: 15rpx 20rpx;
		border-radius: 20rpx;
		border: 1rpx solid #0BBDCB;
		background-color: #0BBDCB;
		font-size: 24rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #fff;
	}

	.noselect {
		padding: 15rpx 20rpx;
		border-radius: 20rpx;
		border: 1rpx solid #999999;
		font-size: 24rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #999999;
	}

	// 图表
	.charts-box2 {
		width: 100%;
		height: 450rpx;
	}

	// 详细数据
	.itembox {
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.06);
	}

	.rightmsg {
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		flex-direction: column;
	}

	.smalltext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.needzdybot {
		padding: 0 30rpx 160rpx;
	}

	// 底部
	.tab_bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 30rpx 30rpx 50rpx;
	}
</style>